<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>云天使</title>
        <meta name="description" content="云天使">
        <meta name="keywords" content="云天使">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>会员管理<small>订单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>会员管理</el-breadcrumb-item>
                    <el-breadcrumb-item>订单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="手机号/套餐名称/订单状态" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="phoneNumber" label="手机号" align="center"></el-table-column>
                        <el-table-column prop="memberName" label="姓名" align="center"></el-table-column>
                        <el-table-column prop="orderStatus" label="订单状态" align="center"></el-table-column>
                        <el-table-column prop="orderType" label="订单类型" align="center"></el-table-column>
                        <el-table-column prop="setmealName" label="套餐名称" align="center"></el-table-column>
                        <el-table-column prop="orderAddress" label="预约地址" align="center"></el-table-column>
                        <el-table-column prop="orderDateString" label="时间" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" icon="el-icon-edit" @click="statusUpdate(scope.row)">&#8194;已开始&#8194;</el-button><br>
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">结果上传<i class="el-icon-upload el-icon--right"></i></el-button><br>
                                <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)">&#12288;删除&#12288;</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增订单" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="姓名" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="手机号" prop="phoneNumber">
                                                        <el-input v-model="formData.phoneNumber"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="身份证号" prop="idCard">
                                                        <el-input v-model="formData.idCard"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="邮箱">
                                                        <el-input v-model="formData.email"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="性别">
                                                        <el-select v-model="formData.sex">
                                                            <el-option label="男" value="1"></el-option>
                                                            <el-option label="女" value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="预约方式">
                                                        <el-select v-model="formData.orderType">
                                                            <el-option label="微信预约" value="微信预约"></el-option>
                                                            <el-option label="电话预约" value="电话预约"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="订单状态">
                                                        <el-select v-model="formData.orderStatus">
                                                            <el-option label="未开始" value="未开始"></el-option>
                                                            <el-option label="已开始" value="已开始"></el-option>
                                                            <el-option label="已结束" value="已结束"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="地址" prop="orderAddress">
                                                        <el-input v-model="formData.orderAddress"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="预约日期(格式2022-01-01)" prop="orderDate">
                                                        <el-input v-model="formData.orderDate"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="套餐信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>套餐编码</th>
                                                    <th>套餐名称</th>
                                                    <th>套餐说明</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData">
                                                    <td>
                                                        <input :id="c.id" v-model="setmealIds" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.code}}</label></td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.remark}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    <!-- 文件上传弹层 -->
                    <div class="add-form">
                        <el-dialog title="文件上传" :visible.sync="dialogFormVisible4Edit">
                            <el-form :model="formData" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="结果上传">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    drag
                                                    ref="upload"
                                                    name="file"
                                                    action="/order/uploadFile"
                                                    :on-success="handleAvatarSuccess"
                                                    :show-file-list="showFileList"
                                                    >
                                                <i class="el-icon-upload"></i>
                                                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                activeName:'first',//添加/编辑窗口Tab标签名称
                pagination: {//分页相关模型数据
                    currentPage: 1,//当前页码
                    pageSize:10,//每页显示的记录数
                    total:0,//总记录数
                    queryString:null//查询条件
                },
                dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                tableData:[],//新增和编辑表单中对应的内容项列表数据
                setmealIds:[],//新增和编辑表单中内容项对应的复选框，基于双向绑定可以进行回显和数据提交
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                showFileList:false,
                rules: {//校验规则
                    name: [{ required: true, message: '会员姓名为必填项', trigger: 'blur' }],
                    phoneNumber: [{ required: true, message: '手机号为必填项', trigger: 'blur' }],
                    idCard: [{ required: true, message: '身份证号为必填项', trigger: 'blur' }],
                    // email: [{ required: true, message: '邮箱为必填项', trigger: 'blur' }],
                    orderAddress: [{ required: true, message: '预约地址为必填项', trigger: 'blur' }],
                    orderDate: [{ required: true, message: '预约日期为必填项', trigger: 'blur' }],
                }
            },

            created() {
                this.findPage();
            },
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            methods: {

                //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
                handleAvatarSuccess(response) {
                    this.showFileList=true;
                    this.$message({
                        message: response.message,
                        type: response.flag ? 'success' : 'error'
                    });
                    //设置模型数据，后续提交ajax请求时会提交到后台最终保存到数据库
                    this.formData.orderFile = response.data;
                },

                //文件上传
                handleEdit() {
                    //发送ajax请求，提交模型数据
                    axios.post("/order/updateFile",this.formData).
                    then((response)=> {
                        //隐藏上传窗口
                        this.dialogFormVisible4Edit = false;
                        this.showFileList=false;
                        if(response.data.flag){
                            this.$message({
                                message: response.data.message,
                                type: 'success'
                            });
                        }else{
                            this.$message.error(response.data.message);
                        }
                    }).finally(()=> {
                        this.findPage();
                        this.$refs.upload.clearFiles();
                    });
                },
                //添加
                handleAdd () {
                    //校验表单输入项是否合法
                    this.$refs['dataAddForm'].validate((valid) => {
                        if (valid) {
                            //表单数据校验通过，发送ajax请求将表单数据提交到后台
                            axios.post(
                                "/order/add?setmealIds=" + this.setmealIds,
                                this.formData
                            )
                                .then((response)=> {
                                    //关闭新增窗口
                                    this.dialogFormVisible = false;
                                    if(response.data.flag){
                                        //新增成功，弹出成功提示
                                        this.$message({
                                            message: response.data.message,
                                            type: 'success'
                                        });
                                    }else{
                                        //新增失败，弹出错误提示
                                        this.$message.error(response.data.message);
                                    }
                                }).finally(()=> {
                                this.findPage();
                            });
                        } else {
                            this.$message.error("表单数据校验失败");
                            return false;
                        }
                    });
                },
                //分页查询
                findPage() {
                    if(this.pagination.queryString!=null&&this.pagination.queryString!=""){
                        this.pagination.currentPage=1;
                    }
                    //分页参数
                    var param = {
                        currentPage:this.pagination.currentPage,//页码
                        pageSize:this.pagination.pageSize,//每页显示的记录数
                        queryString:this.pagination.queryString//查询条件
                    };
                    //请求后台
                    axios.post("/order/findPage",param).then((response)=> {
                        //为模型数据赋值，基于VUE的双向绑定展示到页面
                        this.dataList = response.data.rows;
                        this.pagination.total = response.data.total;
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.dialogFormVisible = true;
                    this.resetForm();
                    //默认切换到第一个标签页（基本信息）
                    this.activeName='first';
                    //重置复选框
                    this.setmealIds = [];
                    //发送ajax请求查询所有内容项信息
                    axios.get("/setmeal/getSetmeal").then((res)=> {
                        if(res.data.flag){
                            //将内容项列表数据赋值给模型数据用于页面表格展示
                            this.tableData = res.data.data;
                        }else{
                            this.$message.error(res.data.message);
                        }
                    });
                },
                // 弹出文件上传窗口
                handleUpdate(row) {
                    this.dialogFormVisible4Edit = true;
                    this.resetForm();
                    this.formData.id=row.id;
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //currentPage为切换后的页码
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                statusUpdate(row){
                    this.$confirm("确认更改吗？","提示",{type:'warning'}).then(()=>{
                        //点击确定按钮时只需此处代码
                        //alert('用户点击的是确定按钮');
                        axios.get("/order/statusUpdate?id=" + row.id).then((res)=> {
                            if(!res.data.flag){
                                //修改失败
                                this.$message.error(res.data.message);
                            }else{
                                //修改成功
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                //调用分页，获取最新分页数据
                                this.findPage();
                            }
                        });
                    }).catch(()=>{
                        this.$message({
                            type:'info',
                            message:'操作已取消'
                        })
                    });
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("确认删除吗？","提示",{type:'warning'}).then(()=>{
                        //点击确定按钮时只需此处代码
                        //alert('用户点击的是确定按钮');
                        axios.get("/order/delete?id=" + row.id).then((res)=> {
                            if(!res.data.flag){
                                //删除失败
                                this.$message.error(res.data.message);
                            }else{
                                //删除成功
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                //调用分页，获取最新分页数据
                                this.findPage();
                            }
                        }).catch((r)=>{
                            this.showMessage(r);
                        });
                    }).catch(()=>{
                        this.$message({
                            type:'info',
                            message:'操作已取消'
                        })
                    });
                }
            }
        })
    </script>
</html>
